package com.harmontronics.hanma.paas.paas.controller;

import com.harmontronics.hanma.paas.paas.annotation.TokenValidSkip;
import java.util.Arrays;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import com.harmontronics.hanma.paas.paas.common.ResponseEntity;
import cn.hutool.core.convert.Convert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import com.harmontronics.hanma.paas.paas.service.HmDataTypeAppService;
import com.harmontronics.hanma.paas.paas.dto.iam.output.HmDataTypeResult;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmDataTypeCreateInput;
import com.harmontronics.hanma.paas.paas.dto.iam.input.HmDataTypeUpdateInput;
import com.harmontronics.hanma.paas.paas.domain.iam.entity.HmDataType;
import com.harmontronics.hanma.paas.paas.utils.ParamUtils;

import javax.validation.Valid;


/**
 * 数据类型
 *
 * @author paas-generator
 * @email paas-generator@harmontronics.com
 * @date 2021-04-07 10:36:37
 */
@RestController
@RequestMapping("/api/hmdatatype")
@Api(tags = {"数据类型"})
public class HmDataTypeController {
    @Autowired
    private HmDataTypeAppService hmDataTypeappService;

    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation("列表")
    public ResponseEntity<List<HmDataTypeResult>> list(@RequestParam Long appId) {
        List<HmDataType> hmDataTypeList = hmDataTypeappService.getList(appId);
        return ResponseEntity.ok(Convert.toList(HmDataTypeResult.class, hmDataTypeList));
    }


    /**
     * 保存
     */
    @PostMapping()
    @ApiOperation("保存")
    public ResponseEntity<String> save(@Valid @RequestBody HmDataTypeCreateInput hmDataTypeInput,BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmDataType hmDataType =BeanUtil.toBean(hmDataTypeInput,HmDataType.class);
        hmDataTypeappService.save(hmDataType);

        return ResponseEntity.ok();
    }

    @PostMapping("batch")
    @ApiOperation(value = "创建数据类型")
    @TokenValidSkip
    public ResponseEntity createBatch(@RequestBody List<HmDataTypeCreateInput> dataTypeDtoList) {
        List<HmDataType> dataTypeList = Convert.toList(HmDataType.class,dataTypeDtoList);
        hmDataTypeappService.createBatch(dataTypeList);
        return ResponseEntity.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation("修改")
    public ResponseEntity<String> update(@Valid @RequestBody HmDataTypeUpdateInput hmDataTypeInput, BindingResult bindingResult) {
        ParamUtils.valid(bindingResult);
        HmDataType hmDataType =BeanUtil.toBean(hmDataTypeInput,HmDataType.class);
        hmDataTypeappService.update(hmDataType);
        return ResponseEntity.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping()
    @ApiOperation("删除")
    public ResponseEntity<String> delete(@RequestBody Long[] ids) {
            hmDataTypeappService.remove(Arrays.asList(ids));

        return ResponseEntity.ok();
    }

}
